package net.helix.engine.viewPath;

import java.util.ArrayList;

import org.lwjgl.opengl.GL11;

import net.helix.core.transform.TransformDimension;
import net.helix.element.Element_Camera;
import net.helix.element.TimelineElement;
import net.helix.element.ViewNodeElement;
import net.helix.engine.timeline.Timeline;
import net.helix.graphics.IRenderable;

public class ViewPath implements IRenderable {
	public ArrayList<ViewNode> path;
	
	// Note, removing viewTransform to remove epic confusion...
	// All "director's eye" transformations will be done in the View class...
	//private Transform viewTransform;
	
	public ViewPath(){
		path = new ArrayList<ViewNode>();
		
		//viewTransform = new Transform();
	}
	/**
	 * Clears the whole list and sets the root of the path.
	 * @param node
	 */
	public void setRoot(ViewNode node){
		path.clear();
		path.add(node);
		
		//this.viewTransform = viewTransform;
		
		
		/*
		if(node.element.getTransform().isFlat()){
			if(transform.isFlat() == false){
				transform = new Transform(true);
				transform.setFlatProjection(engine, height)
			}
			
		}else{
			if(transform.isFlat() == true){
				transform = new Transform();
			}
		}
		System.out.println("set Root, isFlat "+transform.isFlat());
		*/
		
	}
	/**
	 * Adds a node to the path.
	 * @param node
	 */
	public void addNode(ViewNode node){
		path.add(node);
	}
	/**
	 * Removes the last node. (Such as when you exit out of editing a clip)
	 */
	public void pop(){
		path.remove(path.size()-1);
	}
	/**
	 * Returns the root of the list. Usually will return the current scene
	 * you are editing at the moment, or an object from the library if you
	 * double click on it.
	 * @return
	 */
	public ViewNode getRoot(){
		return path.get(0);
	}
	/*
	public void applyTransformation(){
		applyTransformation(path.size());
	}
	public void applyTransformation(int until){
		for(int i=0;i<until;i++){
			path.get(i).element.transform.apply();
		}
	}
	*/
	/**
	 * Get the timeline if it exists at the end of the view path list.
	 * If it does not exist, then it will return null.
	 * @return
	 */
	public Timeline getTimelineAtEnd(){
		ViewNodeElement element = path.get(path.size()-1).element;
		if(element instanceof TimelineElement){
			return ((TimelineElement) element).getTimeline();
		}
		if(element instanceof Element_Camera){
			return ((Element_Camera) element).getTimeline();
		}
		
		return null;
	}
	/**
	 * Get the frame at the end of the view path list. If the last element in the
	 * list does not contain a timelime, it should return 0.
	 * @return
	 */
	public float getFrameAtEnd(){
		return path.get(path.size()-1).frame;
	}
	
	/**
	 *  Renders everything in the view path.
	 */
	@Override
	public void render() {
		getRoot().element.render();
	}
	
	
	public TransformDimension getDimension(){
		if(getRoot() != null){
			return getRoot().element.getTransform().getDimension();
		}else{
			return TransformDimension.TWO;
		}
	}
	
	public String getPathString(){
		String str = "";
		if(path != null){
			int l = path.size();
			for(int i=0;i<l;i++){
				if(i > 0) str += " -> ";
				str += path.get(i).element.getSymbolName();
				
				
			}
			
			
		}
		
		return str;
	}
	
	public int getNumLights(){
		return 0;
	}
	
	/*
	public void setTransform(Transform transform){
		this.viewTransform = transform;
	}
	public Transform getTransform(){
		return viewTransform;
	}
	*/
}
